package com.up.upzl.biz;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Set;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;

import com.google.common.collect.Sets;
import com.up.upzl.UpApplicationTests;
import com.up.upzl.biz.domain.Area;
import com.up.upzl.biz.domain.Road;
import com.up.upzl.biz.repository.AreaRepository;
import com.up.upzl.biz.repository.RoadRepository;

import lombok.extern.slf4j.Slf4j;

/**
 * District T
 * 
 * @author geeks
 *
 */
@Slf4j
public class RoadT extends UpApplicationTests {

	@Autowired
	private RoadRepository roadRepository;

	@Autowired
	private AreaRepository areaRepository;

	/**
	 * 导入道路
	 * 
	 * @throws Exception
	 */
	@Test
	public void existsByIdAndCitySitesSiteCode() throws Exception {
		log.info("Mission start ...");
		BufferedReader reader = new BufferedReader(new InputStreamReader(RoadT.class.getResourceAsStream("roads.txt")));

		Set<String> roadSet = Sets.newHashSet();
		String temp;
		while ((temp = reader.readLine()) != null) {
			roadSet.add(temp);
		}
		log.info("{}", roadSet.size());

		roadSet.forEach(rd -> {
			String[] roadArray = rd.split(" ");
			Area area = areaRepository.findByName(roadArray[1]);
			Assert.notNull(area, "未找到地区 => " + roadArray[1]);

			Road road = new Road();
			road.setArea(area);
			road.setName(roadArray[0]);

			roadRepository.save(road);
		});

		log.info("Mission complete ...");
	}

}
